//
//  MHAlert2.swift
//  YueChat
//
//  Created by IMac on 2024/10/12.
//

import UIKit
//import PKHUD
import KRProgressHUD

enum AlertType {
    case warn
    case remind
}

typealias OkBlock = (() -> Void)?

class MHAlert: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    class func showMessage(_ message : String){
        KRProgressHUD.set(style: .black)
        KRProgressHUD.showMessage(message)
//        HUD.registerForKeyboardNotifications()
//        
//        HUD.dimsBackground = false
//        HUD.allowsInteraction = false
//        HUD.flash(.label(message), delay: 1.0) { _ in
//            
//        }
    }
    
    class func showLoadingStr(_ message : String){
//        HUD.flash(.labeledProgress(title: message, subtitle: ""), delay: 2.0)
        KRProgressHUD.show(withMessage: message)
        let delay = DispatchTime.now() + 2
        DispatchQueue.main.asyncAfter(deadline: delay) {
            KRProgressHUD.dismiss {
                print("dismiss() completion handler.")
            }
        }
    }

    
    class func showLoading(){
//        HUD.show(.progress)
        KRProgressHUD.set(style: .black)
        KRProgressHUD.show(withMessage: "加载中...")
    }
    
    class func dismiss(){
//        HUD.hide()
        KRProgressHUD.dismiss()
    }
    
   
    class func showCustomAlert(_ message: String, block: ((UIAlertAction) -> Void)?) {
        var topVC = UIApplication.shared.keyWindow?.rootViewController
        if let presentedVC = topVC?.presentedViewController {
            topVC = presentedVC
        }

        let alert = UIAlertController(title: "", message: message, preferredStyle: .alert)
        
        let okAction = UIAlertAction(title: NSLocalizedString("Sure", comment: ""), style: .default, handler: block)
        let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil)

        alert.addAction(cancelAction)
        alert.addAction(okAction)

        topVC?.present(alert, animated: true, completion: nil)
    }

    
    class func showSheetAlert(_ message: String, block: ((UIAlertAction) -> Void)?) {
        var topVC = UIApplication.shared.keyWindow?.rootViewController
        if let presentedVC = topVC?.presentedViewController {
            topVC = presentedVC
        }

        let alert = UIAlertController(title: "", message: message, preferredStyle: .actionSheet)
        
        let okAction = UIAlertAction(title: NSLocalizedString("Sure", comment: ""), style: .default, handler: block)
        let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil)

        alert.addAction(cancelAction)
        alert.addAction(okAction)

        topVC?.present(alert, animated: true, completion: nil)
    }

}
